package cn.itcast.controller;

import cn.itcast.entity.Result;
import cn.itcast.massage.MessageConstant;
import cn.itcast.massage.RedisMessageConstant;
import cn.itcast.utils.POIUtils;
import cn.itcast.utils.SMSUtils;
import cn.itcast.utils.ValidateCodeUtils;
import com.aliyuncs.exceptions.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validation")
public class ValidationController {
    @Autowired
    private JedisPool jedisPool;
    @RequestMapping("/order")
    public Result order(String telephone){
        // 生成验证码
        Integer generateValidateCode = ValidateCodeUtils.generateValidateCode(4);
        // 发送验证码
        try {
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,String.valueOf(generateValidateCode));
            // 将验证码存到redis,5分钟有效
            jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,300,String.valueOf(generateValidateCode));
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }

    @RequestMapping("/send4Login")
    public Result send4Login(String telephone){
        // 生成验证码
        Integer generateValidateCode = ValidateCodeUtils.generateValidateCode(6);
        // 发送验证码
        try {
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,String.valueOf(generateValidateCode));
            // 将验证码存到redis,5分钟有效
            jedisPool.getResource().setex(telephone+RedisMessageConstant.SENDTYPE_LOGIN,300,String.valueOf(generateValidateCode));
            return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
}
